package cn.xpg.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xpg.entity.Intoproduct;
import cn.xpg.service.IntoproductService;
import cn.xpg.tools.ExportExcelUtil;
import cn.xpg.tools.PageSupport;

@Controller
public class IntoproductController {
	@Resource(name = "intoproductService")
	private IntoproductService intoproductService;

	// 分页查询入库单信息
	@RequestMapping("/getIntoproductList.do")
	public String getIntoproductList(HttpServletRequest request) {
		String product_name = request.getParameter("product_name");
		String pageIndex = request.getParameter("pageIndex");
		String sname = request.getParameter("sname");
		int pageSize = 3;
		int currentPageNo = 1;
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		int totalCount = 0;
		totalCount = intoproductService.getIntoproductCount(product_name);
		System.out.println("查询入库单信息！！！！！！！！！！————>getIntoproductList");

		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();

		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Intoproduct> intoproductList = null;
		intoproductList = intoproductService.getIntoproductList(product_name, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("intoproductList", intoproductList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("product_name", product_name);
		if (sname.equals("list")) {
			return "/jsp/jinxiaocun/entry_list";
		} else {
			return "/jsp/jinxiaocun/entry_statistics";
		}
	}

	// 导入excel
	@RequestMapping("/getIntoproductListTOExcel.do")
	public void getIntoproductListTOExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String product_name = request.getParameter("product_name");
		if (product_name == null)
			product_name = "";
		List<Intoproduct> intoproductList = intoproductService.getIntoproductListTOExcel(product_name);
		ExportExcelUtil ex = new ExportExcelUtil();
		String title = "Warehouse receipt";
		String[] headers = { "入库单编号", "产品编号", "供应商编号", "支付类型", "进货时间", "录单人", "数量", "描述", "所属店铺" };
		List<String[]> dataset = new ArrayList<String[]>();
		for (int i = 0; i < intoproductList.size(); i++) {
			Intoproduct intoproduct = intoproductList.get(i);
			dataset.add(new String[] { String.valueOf(intoproduct.getIntoproduct_id()),
					String.valueOf(intoproduct.getProduct_id()), String.valueOf(intoproduct.getSupplier_id()),
					intoproduct.getPay_type(), String.valueOf(intoproduct.getIntoproduct_time()),
					String.valueOf(intoproduct.getEmployee_id()), String.valueOf(intoproduct.getIntoproduct_num()),
					intoproduct.getIntoproduct_note(), String.valueOf(intoproduct.getStore_id()) });
		}
		/*
		 * OutputStream out = null; try { out = new FileOutputStream("C://output.xls");
		 * ex.exportExcel(title,headers, dataset, out); out.close(); } catch (Exception
		 * e) { e.printStackTrace(); }
		 */
		OutputStream out = null;// 创建一个输出流对象
		try {
			out = response.getOutputStream();//
			response.setHeader("Content-disposition", "attachment; filename=" + "图书信息记录.xls");// filename是下载的xls的名，建议最好用英文
			response.setContentType("application/msexcel;charset=UTF-8");// 设置类型
			response.setHeader("Pragma", "No-cache");// 设置头
			response.setHeader("Cache-Control", "no-cache");// 设置头
			response.setDateHeader("Expires", 0);// 设置日期头
			String rootPath = request.getSession().getServletContext().getRealPath("/");
			ex.exportExcel(rootPath, title, headers, dataset, out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 新增入库单
	@RequestMapping("addIntoproduct.do")
	public String addIntoproduct(@Valid Intoproduct Intoproduct) {
		System.out.println("新增入库单信息！！！！！！！！！！————>addIntoproduct");
		if (intoproductService.addIntoproduct(Intoproduct) > 0) {
			return "redirect:/getIntoproductList.do";
		} else {
			return "/jsp/user/manage_qy";
		}
	}

	// 获取入库单Id
	@RequestMapping("getIntoproductId.do")
	public String getIntoproductId(HttpServletRequest request) {
		System.out.println("获取入库单Id！！！！！！！！！！————>getIntoproductId");
		int intoproduct_id = Integer.parseInt(request.getParameter("intoproduct_id"));
		Intoproduct intoproduct = intoproductService.getIntoproductId(intoproduct_id);
		request.setAttribute("intoproduct", intoproduct);
		return "/jsp/jinxiaocun/entry_update";
	}

	// 修改入库单
	@RequestMapping("updateIntoproduct.do")
	public String updateIntoproduct(@Valid Intoproduct intoproduct, HttpServletRequest request) {
		System.out.println("修改入库单信息！！！！！！！！！！————>updateIntoproduct");
		if (intoproductService.updateIntoproduct(intoproduct) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getIntoproductList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("intoproduct", intoproduct);
			return "/jsp/jinxiaocun/entry_update";
		}
	}

	// 删除入库单
	@RequestMapping("deleteIntoproduct.do")
	public String deleteIntoproduct(HttpServletRequest request) {
		System.out.println("删除入库单信息！！！！！！！！！！————>deleteIntoproduct");
		int intoproduct_id = Integer.valueOf(request.getParameter("intoproduct_id"));
		if (intoproductService.deleteIntoproduct(intoproduct_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getIntoproductList.do";
	}
}
